package cases;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;
import cn.hutool.db.Db;
import cn.hutool.db.Entity;
import cn.hutool.db.ds.DSFactory;
import lombok.SneakyThrows;
import utils.SmallTool;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.IntStream;

public class UpCases {
    @SneakyThrows
    public static void main(String[] args) {
        // 更新本地cases 每次老板要数据都要服务器复制 修复 压缩下载 解压 很耗时间，20240606 这次采用本地更新cases 方式进行


        SmallTool.printMemory();
        Db serverNcbidao4 = Db.use(DSFactory.get("server_ncbidao4"));
        Db localArticleaddpm = Db.use(DSFactory.get("local_articleaddpm"));
        String fromTable = "cases";
        String localTable = "caseslocal";
        int localMax = localArticleaddpm.queryOne("select max(caseid) as ma from " + localTable).getInt("ma");
        int fromMax = serverNcbidao4.queryOne("select max(caseid) as ma from " + fromTable).getInt("ma");
        SmallTool.printMess("本地最大caseid+" + localMax);
        SmallTool.printMess("服务器最大caseid+" + fromMax);
        List<Integer> caseAllToDown = new ArrayList<>(10000000);
        if (fromMax > localMax) {
            IntStream.range(localMax, fromMax).forEach(as -> {
                caseAllToDown.add(as);
            });
        }
        List<List<Integer>> split = ListUtil.split(caseAllToDown, 100);
        SmallTool.printMess("全部需要入库的数量是+" + caseAllToDown.size());
        SmallTool.printMess("全部需要入库的页码数量是+" + split.size());
        for (List<Integer> integers : split) {
            String join = CollUtil.join(integers, ",");
            try {
                List<Entity> query = serverNcbidao4.query(" select * from " + fromTable + " where caseid in ( " + join + " )");
                if (query.size() > 0) {
                    for (Entity entity : query) {
                        try {
                            entity.setTableName(localTable);
                            localArticleaddpm.insert(entity);
                        } catch (Exception e2) {
                            e2.printStackTrace();
                        }
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
